// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.login;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dengues.core.resource.DenguesProject;
import org.dengues.core.resource.DenguesProjectFactory;
import org.dengues.model.project.UserType;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-3-13 qiang.zhang $
 * 
 */
public class LoginDenguesComposite extends Composite {

    private DenguesProject[] readProjects;

    private Button buttonOK;

    private Button buttonCancel;

    private final static int F_BUTTON_WIDTH_HINT = 80;

    private Map<String, List<String>> userProjs;

    private ListViewer projListViewer;

    private ListViewer userListViewer;

    /**
     * Qiang.Zhang.Adolf@gmail.com LoginDenguesComposite constructor comment.
     * 
     * @param parent
     * @param style
     */
    public LoginDenguesComposite(Composite parent, int style) {
        super(parent, style);
        GridLayout layout = new GridLayout(1, false);
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        layout.verticalSpacing = 0;
        setLayout(layout);
        init();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "init".
     */
    private void init() {
        initData();
        topComposite();
        Label titleBarSeparator = new Label(this, SWT.HORIZONTAL | SWT.SEPARATOR);
        titleBarSeparator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        // Create the blank label
        createUILabelBlank();
        // Create the OK button
        createUIButtons();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "initData".
     */
    private void initData() {
        readProjects = DenguesProjectFactory.readProjects();
        userProjs = new HashMap<String, List<String>>();
        for (DenguesProject denguesProject : readProjects) {
            List<String> list = userProjs.get(denguesProject.getName());
            if (list == null) {
                list = new ArrayList<String>();
            }
            EList<UserType> user = denguesProject.getProjectType().getUser();
            for (UserType userType : user) {
                list.add(userType.getEMail() + " (" + userType.getName() + ")");
            }
            userProjs.put(denguesProject.getName(), list);
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "topComposite".
     */
    private void topComposite() {
        Composite composite = new Composite(this, SWT.NONE);
        GridLayout gridLayout = new GridLayout(3, false);
        gridLayout.marginHeight = 0;
        gridLayout.marginWidth = 0;
        composite.setLayout(gridLayout);
        GridData gridData = new GridData(GridData.FILL_BOTH);
        composite.setLayoutData(gridData);
        createProjectList(composite);
        createUserList(composite);
        createInfoComposite(composite);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createInfoComposite".
     * 
     * @param composite2
     */
    private void createInfoComposite(Composite composite2) {
        Composite composite = new Composite(composite2, SWT.NONE);
        GridLayout gridLayout = new GridLayout(2, false);
        gridLayout.marginHeight = 0;
        gridLayout.marginWidth = 0;
        composite.setLayout(gridLayout);
        GridData gridData = new GridData(GridData.FILL_BOTH);
        composite.setLayoutData(gridData);
        createOperateButtons(composite);
        createInformations(composite);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createInformations".
     * 
     * @param composite
     */
    private void createInformations(Composite composite) {
        Group group = new Group(composite, SWT.NONE);
        group.setText("All Information");
        group.setLayout(new GridLayout());
        group.setLayoutData(new GridData(GridData.FILL_BOTH));
        group.setEnabled(false);

    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createOperateButtons".
     * 
     * @param composite
     */
    private void createOperateButtons(Composite composite) {
        Composite opComp = new Composite(composite, SWT.NONE);
        opComp.setLayout(new GridLayout());
        opComp.setLayoutData(new GridData(GridData.FILL_VERTICAL));
        Button opProj = new Button(opComp, SWT.PUSH);
        opProj.setText("Operate Project");
        Button shareProj = new Button(opComp, SWT.PUSH);
        shareProj.setText("Share Project");
        opProj.addSelectionListener(new SelectionAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                LoginDenguesDialog denguesDialog = new LoginDenguesDialog(new Shell());
                int open = denguesDialog.open();
                refreshAll();
            }

        });
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "refreshAll".
     */
    protected void refreshAll() {
        initData();
        projListViewer.setInput(userProjs.keySet().toArray(new String[0]));
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createProjectList".
     * 
     * @param composite
     */
    private void createProjectList(Composite composite) {
        Group group = new Group(composite, SWT.NONE);
        group.setText("Project List");
        group.setLayout(new GridLayout());
        group.setLayoutData(new GridData(GridData.FILL_VERTICAL));
        projListViewer = new ListViewer(group, SWT.V_SCROLL | SWT.H_SCROLL);
        projListViewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
        LoginListProvider listLabelProvider = new LoginListProvider();
        projListViewer.setLabelProvider(listLabelProvider);
        projListViewer.setContentProvider(listLabelProvider);
        projListViewer.setInput(userProjs.keySet().toArray(new String[0]));
        projListViewer.addSelectionChangedListener(new ISelectionChangedListener() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
             */
            public void selectionChanged(SelectionChangedEvent event) {
                IStructuredSelection selection = (IStructuredSelection) event.getSelection();
                List<String> list = userProjs.get(selection.getFirstElement());
                if (list != null) {
                    userListViewer.setInput(list);
                }
            }
        });
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createUserList".
     * 
     * @param composite
     */
    private void createUserList(Composite composite) {
        Group group = new Group(composite, SWT.NONE);
        group.setText("User List");
        group.setLayout(new GridLayout());
        group.setLayoutData(new GridData(GridData.FILL_VERTICAL));
        userListViewer = new ListViewer(group, SWT.V_SCROLL | SWT.H_SCROLL);
        userListViewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
        LoginListProvider listLabelProvider = new LoginListProvider();
        userListViewer.setLabelProvider(listLabelProvider);
        userListViewer.setContentProvider(listLabelProvider);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createUIButtons".
     */
    private void createUIButtons() {
        Composite composite = new Composite(this, SWT.NONE | SWT.RIGHT);
        GridLayout gridLayout = new GridLayout(2, false);
        gridLayout.marginHeight = 0;
        gridLayout.marginWidth = 0;
        composite.setLayout(gridLayout);
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
        gridData.horizontalAlignment = GridData.HORIZONTAL_ALIGN_END;
        composite.setLayoutData(gridData);
        // Label label = new Label(composite, SWT.BORDER);
        // label.setVisible(false);
        // label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        // Create the button
        buttonOK = new Button(composite, SWT.PUSH);
        buttonOK.setText("OK");
        // Configure layout data
        GridData data = new GridData();
        data.widthHint = F_BUTTON_WIDTH_HINT;
        // data.verticalIndent = 10;
        buttonOK.setLayoutData(data);

        // Create the button
        buttonCancel = new Button(composite, SWT.PUSH);
        buttonCancel.setText("Cancel");
        // Configure layout data
        data = new GridData();
        data.widthHint = F_BUTTON_WIDTH_HINT;
        // data.verticalIndent = 10;
        buttonCancel.setLayoutData(data);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createUILabelBlank".
     */
    private void createUILabelBlank() {
        Label label = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
        label.setVisible(false);
    }

    /**
     * Getter for buttonOK.
     * 
     * @return the buttonOK
     */
    public Button getButtonOK() {
        return this.buttonOK;
    }

    /**
     * Getter for buttonCancel.
     * 
     * @return the buttonCancel
     */
    public Button getButtonCancel() {
        return this.buttonCancel;
    }

}
